/***********************************************************************
 * 	Copyright @ 2011 Zhejiang Yutian Technology Co.,Ltd
	==================================================================
	Project:        LaYouNet
	FileName:       NavTabBar.java
	Describtion:    文件描述
	Author:         xufei
	Create Date:    2014-7-15 上午11:41:00
 *********************************************************************/
package com.jason.platform.supervision.widght;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.hzlj.supervision.changsha.R;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * @ClassName: NavTabBar
 * @Description: 可滑动导航栏
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-27 15:39:51
 */
public class NavTabBar extends ElasticHScrollView{

	private boolean mFinishAdd = true;;
	/** 上下文*/
	private Context mContext;
	/** 当前焦点位置*/
	private int mCurSelected = 0;
	/** 导航栏布局*/
	private LinearLayout mNavLayout;
	/** 指示器*/
	private ImageView mSelctedIndexImg;
	/** 事件回调*/
	private OnNavTabBarSelectedListener mOnNavTabBarSelectListener;
	/** 文字颜色状态#b4c833*/
	private int[] mTextColorState = new int[]{0xff585858, 0xff03A9F5};
	/** 焦点背景图片*/
//	private int mSelectedBgRes = R.drawable.transparent;
	private int mSelectedBgRes = R.drawable.transparent;
	/** 是否显示滑动指示器*/
	private boolean mShowIndexImg = true;

	public int totalLength=0;
	
	public void setCurSelected(int mCurSelected) {
		this.mCurSelected = mCurSelected;
	}
	public LinearLayout getmNavLayout() {
		return mNavLayout;
	}

	public NavTabBar(Context context) {
		super(context);
		init();
	}
	
	public NavTabBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		mContext = getContext();
		if(isInEditMode()){
			return;
		}
		RelativeLayout container = (RelativeLayout)LinearLayout.inflate(mContext, R.layout.widget_navbar_layout, null);
		mSelctedIndexImg = (ImageView)container.findViewById(R.id.navbar_selected_index_image);
		mNavLayout = (LinearLayout)container.findViewById(R.id.navbar_layout);
		addView(container);
	}
	
	private void addNavChildView(View view) {
		mNavLayout.addView(view);
		view.setTag(mNavLayout.getChildCount()-1);
		view.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				setSelected((Integer)v.getTag(), true);
			}
		});
	}
	
	public void addNavChildText(String text, int dividerRes) {
    	View view = LinearLayout.inflate(mContext,R.layout.widget_navbar_tab_item,null);
    	TextView tv = (TextView)view.findViewById(R.id.item_text);
    	tv.setText(text);
    	if (dividerRes != 0)
    		tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, dividerRes, 0);
    	
    	addNavChildView(view);
    	if(mNavLayout.getChildCount() == mCurSelected+1 && mShowIndexImg)
    		view.findViewById(R.id.selected_index_image).setVisibility(View.GONE);
	}
	
	public void finishAdd(int width, int selectedBgRes) {
		if (!mFinishAdd)
			return;
		if (selectedBgRes != 0){
			this.mSelectedBgRes = selectedBgRes;
		}
		mFinishAdd = false;
		int count = mNavLayout.getChildCount();
		if(count == 0)
			return;
		if(getNavChildView(count-1).getRight() < getRight()) {
			for(int i=0;i<count;i++) {
				View view = getNavChildView(i);
				
				TextView tv = (TextView)view.findViewById(R.id.item_text);
				view.setLayoutParams(new LinearLayout.LayoutParams(width/count,LinearLayout.LayoutParams.MATCH_PARENT));
				if (mCurSelected == i) {
			    	tv.setSelected(true);
//			    	tv.setBackgroundResource(mSelectedBgRes);
			    	
				} else {
					tv.setSelected(false);
//			    	tv.setBackgroundResource(0);
				}
				
				
				
				if (i == count-1){
					tv.setCompoundDrawables(null, null, null, null);
				}
			}
		}
	}
	
	public void setSelected(int position, boolean callback) {
    	if(mCurSelected == position)
    		return;
    	for(int i=0;i<mNavLayout.getChildCount();i++) {
    		View view = mNavLayout.getChildAt(i);
    		if(i == position) {
    			final TextView tv = (TextView)view.findViewById(R.id.item_text);
    			
				if(view.getRight() > getRight()) {
					scrollBy(view.getRight()-getRight(), 0);
				}else if(view.getLeft() < getScrollX()) {
					scrollBy(view.getLeft()-getScrollX(), 0);
				}
				
    			View lastSelectedView = mNavLayout.getChildAt(mCurSelected);
    			if (mShowIndexImg){
    				view.findViewById(R.id.selected_index_image).setVisibility(View.GONE);
    				mSelctedIndexImg.setVisibility(View.GONE);
        			mSelctedIndexImg.getLayoutParams().width = lastSelectedView.getWidth();
        			ObjectAnimator anim = ObjectAnimator.ofFloat(mSelctedIndexImg, "translationX", lastSelectedView.getLeft(), view.getLeft()).setDuration(100);
        			anim.start();
        			anim.addListener(new AnimatorListener(){
						public void onAnimationCancel(Animator arg0) {
							tv.setSelected(false);
						}
						public void onAnimationEnd(Animator arg0) {
							tv.setSelected(true);
						}
						public void onAnimationRepeat(Animator arg0) {
						}
						public void onAnimationStart(Animator arg0) {
						}
					});
    			} else{
	    			tv.setSelected(true);
//	    			tv.setBackgroundResource(mSelectedBgRes);
    			}
				
				if (callback && mOnNavTabBarSelectListener != null)
					mOnNavTabBarSelectListener.onTabSelected(position);
    		}else if(i == mCurSelected) {
    			view.setSelected(false);
    			TextView tv = (TextView)view.findViewById(R.id.item_text);
    			tv.setSelected(false);
//    			tv.setBackgroundResource(mSelectedBgRes);
    			view.findViewById(R.id.selected_index_image).setVisibility(View.GONE);
    		}
    	}
    	mCurSelected = position;
	}
	
	public int getCurrentSelectedPosition() {
		return mCurSelected;
	}
	
	public View getNavChildView(int position) {
		if(position >= 0 && position < mNavLayout.getChildCount())
			return mNavLayout.getChildAt(position);
		return null;
	}
	
	public void reset(){
		mFinishAdd = true;
	}
	
	/**
	 * NavTabBar事件监听回调类
	 * @author dailiang
	 * @date 2014-4-17
	 * @copyright YTXT
	 * @version 1.0
	 */
	public static interface OnNavTabBarSelectedListener {
		
		/**
		 * 点击导航栏某一项时调用
		 * @param position
		 */
		public void onTabSelected(int position);
	}
	
	public void setOnNavTabSelectedListener(OnNavTabBarSelectedListener listener) {
		this.mOnNavTabBarSelectListener = listener;
	}
	
	public boolean isShowIndexImg() {
		return mShowIndexImg;
	}

	public void setShowIndexImg(boolean mShowIndexImg) {
		this.mShowIndexImg = mShowIndexImg;
	}

	public void setTextColorState(int[] mTextColorState) {
		this.mTextColorState = mTextColorState;
	}
	
}
